#!usr/bin/env python
# -*- coding:utf-8 -*-
# user nanme: Mr.zhang
# data time : 2022/12/13   4:46 下午
# project   : start_java.py.py
# IDE       : PyCharm
# email     :17600960626@163.com

import logging
import os
import time
from multiprocessing import Pool
import jpype
from lib.read_file import Corpus
from lib.read_sql import db
from public.sql_language import sql_dev



class run_star(Corpus):

    def __init__(self,patterns):
        if patterns == "1":
            self.DataLearn_part("com.kikatech.RunHWWord")
        elif patterns == "2":
            self.DataLearn_part("com.kikatech.RunHWWord")
        elif patterns == "0":
            self.Fuzzed_part("com.kikatech.RunHWCorrectWord")
        else:
            print("错误")

    def start_java_datalearn(self,modes, data_path, device_id, language):
        jarpath = self.base_path("/data/java_project/javaprocess.jar")
        print('now id3 : %s,parent id : %s' % (os.getpid(), os.getppid()))
        try:
            jvmpath = jpype.getDefaultJVMPath()
            jpype.startJVM(jvmpath, "-ea","-Djava.class.path=%s" %jarpath)  # "-Djava.ext.dirs=%s" % jarpath1
            jclassstart = jpype.JClass(modes)
            time.sleep(1)
            jclassstart.main([data_path, device_id, language])
            jpype.shutdownJVM()
        except Exception as e:
            logging.exception(e)


    def DataLearn_part(self,modes):
        """
        输入效率测试
        :return:
        """
        try:
            p = Pool(len(sql_dev()))
        except Exception as e:
            logging.error(e, '运行错误')
        else:
            for i in sql_dev():
                # os.system('adb -s %s logcat -b all -c' % i[1])
                os.system('adb -s %s shell am start -S com.xinmei365.emptyinput/.MainActivity' % i[1])
                time.sleep(2)
                p.apply_async(self.start_java_datalearn, args=(modes,i[0], i[1], i[2]))
                time.sleep(3)
                logging.warning('测试语料及数据源:%s,%s,%s' % (i[0], i[1], i[2]))
            p.close()
            p.join()

            for j in sql_dev():
                sql_nub = 'SELECT numbers FROM master_table WHERE devices="%s"' % j[1]
                nums = db.selectDb(sql_nub)
                sql4 = 'UPDATE zhu_lang SET code = 1,numbers=%s WHERE devices="%s"' % (nums[1][0][0], j[1])
                sql5 = 'UPDATE dev_table SET code = 2 WHERE devices="%s"' % j[1]
                db.updateDb(sql4)
                db.updateDb(sql5)
                logging.warning('工程ID:%s启动正常，需手动停止控制台' % j[1])
                sql6 = 'SELECT * FROM master_table WHERE devices="%s"' % j[1]
                db.selectDb(sql6)


    def Fuzzed_part(self,modes):
        """
        纠错测试
        :return:
        """
        try:
            p = Pool(len(sql_dev()))
        except Exception as e:
            logging.error(e, '运行错误')
        else:
            for i in sql_dev():
                # os.system('adb -s %s logcat -b all -c'% i[1])
                os.system('adb -s %s shell am start -S com.xinmei365.emptyinput/.MainActivity' % i[1])
                time.sleep(2)
                p.apply_async(self.start_java_datalearn, args=(modes,i[0], i[1], i[2]))
                logging.warning('测试语料及数据源:%s,%s,%s' % (i[0], i[1], i[2]))
            p.close()
            p.join()

            for j in sql_dev():
                sql_nub = 'SELECT numbers FROM master_table WHERE devices="%s"' % j[1]
                nums = db.selectDb(sql_nub)
                sql4 = 'UPDATE zhu_lang SET code = 1,numbers=%s WHERE devices="%s"' % (nums[1][0][0], j[1])
                sql5 = 'UPDATE zhu_lang SET code = 1 WHERE devices="%s"' % j[1]
                db.updateDb(sql4)
                db.updateDb(sql5)
                logging.warning('工程ID:%s启动正常，需手动停止控制台' % j[1])
                sql6 = 'SELECT * FROM master_table WHERE devices="%s"' % j[1]
                db.selectDb(sql6)


#
if __name__ == '__main__':
    run=run_star("1")#手动运行

